﻿namespace EasyWeb.Data.EntityFramework
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.Threading.Tasks;
    using EasyWeb.Data.EntityFramework.Models;

    [Export(typeof(IEWSelectionItemDefinitionDao))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class EWSelectionItemDefinitionDao : EWDao, IEWSelectionItemDefinitionDao
    {
        public virtual async Task<IEnumerable<EWSelectionItemDefinition>> SelectAsync()
        {
            using (var transaction = EWHelpers.Data.BeginTransaction())
            {
                var result = await SelectCoreAsync<EWSelectionItemDefinition, EWSelectionItemDefinitionModel>();

                transaction.Complete();

                return result;
            }
        }

        public virtual async Task CreateAsync(EWSelectionItemDefinition domain)
        {
            EWGuard.IsNotNull(domain, "domain");

            using (var transaction = EWHelpers.Data.BeginTransaction())
            {
                await CreateCoreAsync<EWSelectionItemDefinition, EWSelectionItemDefinitionModel>(domain);

                transaction.Complete();
            }
        }

        public virtual async Task UpdateAsync(EWSelectionItemDefinition domain)
        {
            EWGuard.IsNotNull(domain, "domain");

            using (var transaction = EWHelpers.Data.BeginTransaction())
            {
                await UpdateCoreAsync<EWSelectionItemDefinition, EWSelectionItemDefinitionModel>(domain);

                transaction.Complete();
            }
        }

        public virtual async Task DeleteAsync(Guid id, byte[] timestamp)
        {
            EWGuard.IsNotNull(timestamp, "timestamp");

            using (var transaction = EWHelpers.Data.BeginTransaction())
            {
                await DeleteCoreAsync<EWSelectionItemDefinitionModel>(id, timestamp);

                transaction.Complete();
            }
        }
    }
}
